package org.itboys.core.controller.front.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.core.dto.member.SundayMemberDto;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.service.mongo.front.SundayFrontMemberService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：zzq
 * 时间：2018/10/18
 * 描述：用户信息
 */
@Controller
@RequestMapping("/sunday/small/member")
@Api(value = "member", description = "用户")
public class SundaySmallMemberController extends BaseController {
    @Autowired
    private SundayFrontMemberService memberService;
    @Autowired
    private ResourceHolder resourceHolder;

    /**
     * @param request
     * @param response
     * @param code
     * @throws IOException
     */
    @RequestMapping(value = "/loginWithCode", method = {RequestMethod.POST})
    @ApiOperation(value = "用户登陆（只获取openId）")
    public void login(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "小程序登录code") @RequestParam(value = "code", required = true) String code
    ) throws IOException {
        SundayMember member = memberService.loginWithCode(code);
        ResponseMessage.success(member);
    }

    /**
     * @param request
     * @param response
     * @param memberId
     * @param code
     * @param encryptedData
     * @param iv
     * @param mobile
     * @param validateCode
     * @throws Exception
     */
    @RequestMapping(value = "/loginWithUnion", method = {RequestMethod.POST})
    @ApiOperation(value = "用户通过unionID登录")
    public void loginWithUnion(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "当前用户id") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(value = "小程序登录code") @RequestParam(value = "code", required = true) String code,
            @ApiParam(value = "加密数据") @RequestParam(value = "encryptedData", required = true) String encryptedData,
            @ApiParam(value = "iv") @RequestParam(value = "iv", required = true) String iv,
            @ApiParam(value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(value = "验证码") @RequestParam(value = "validateCode", required = true) String validateCode
    ) throws Exception {
        SundayMember member = memberService.loginWithUnion(memberId, code, encryptedData, iv, mobile, validateCode);
        ResponseMessage.success(member);
    }

    
    @RequestMapping(value = "/findOne", method = {RequestMethod.POST})
    @ApiOperation(value = "查找单个")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户id") @RequestParam(value = "memberId", required = true) Long memberId
    ) {
        SundayMemberDto member = memberService.findOne(memberId);
        ResponseMessage.success(member);
    }
}
